//
// Created by user on 6/22/19.
//

#include <opencv2/opencv.hpp>
#include <iostream>

/**
 * @brief The intersection of two lines
 * @param p11, p12 线段1的端点
 * @param p21, p22 线段2的端点
 */
cv::Point2f IntersectionOfTwoLines(cv::Point2f p11, cv::Point2f p12, cv::Point2f p21, cv::Point2f p22)
{
    assert(std::abs(p11.x - p12.x) > 1e-5);
    assert(std::abs(p21.x - p22.x) > 1e-5);

    cv::Point2f vanish;
    float kx1=0, kx2=0;
    kx1 = (p11.y - p12.y)/(p11.x - p12.x);
    kx2 = (p21.y - p22.y)/(p21.x - p22.x);
    std::cout<<"kx  is "<<kx1<<" "<<kx2<<std::endl;
    vanish.x = (kx1*p11.x - p11.y - kx2*p21.x +p21.y)/(kx1 - kx2);
    vanish.y = kx1*(vanish.x - p12.x) + p12.y;

    return vanish;
}

/**
 * @brief point of this line based two points
 * @param p1, p2 线段两个端点
 * @param xy 待求点的横坐标或者纵坐标
 * @param is_x true:x, false:y
 */
cv::Point2f GetPointOnLine(cv::Point2f p1, cv::Point2f p2, float xy, bool is_x)
{
    assert(std::abs(p1.x - p2.x) > 1e-5);
    float p;
    float k=0;

    k = (p1.y - p2.y)/(p1.x - p2.x);
    p = is_x ? k*(xy - p1.x) + p1.y : (xy - p1.y)/k + p1.x;

    cv::Point2f pt;
    pt = is_x ? cv::Point2f(xy, p) : cv::Point2f(p, xy);
    return pt;
}

/**
 * @brief 判断三点是否共线
 */
bool IsCollinear(cv::Point2f p1, cv::Point2f p2, cv::Point2f p3)
{
    if (std::abs((p2.x - p1.x)*p3.y - (p2.y - p1.y)*p3.x - p1.y*p2.x + p1.x*p2.y) < 1e-5)
        return true;
    else
        return false;
}


/**
 * @brief 双线性灰度插值
 */
double GetBilinearInterpolatedValue(const cv::Mat& img, const cv::Point2f& pt) {
    uchar* d = & img.data[ int(pt.y)*img.step+int(pt.x) ];
    double xx = pt.x - floor(pt.x);
    double yy = pt.x - floor(pt.y);
    return  (( 1-xx ) * ( 1-yy ) * double(d[0]) +
             xx* ( 1-yy ) * double(d[1]) +
             ( 1-xx ) *yy* double(d[img.step]) +
             xx*yy*double(d[img.step+1]))/255.0;
}





int main()
{
    return 0;
}